<script setup>
import { defineSchedule } from "../store/scheduleStore";
import { defineUser } from "../store/userStore";
import { onMounted } from "vue";
import request from '../utils/request.js'

let sysUser = defineUser()
let sysSchedule = defineSchedule()

onMounted(async ()=>{
    showSchedule()
})

async function showSchedule(){
    let {data} = await request.get('schedule/findAllSchedule',{params:{"uid":sysUser.uid}})
    sysSchedule.itemList = data.data.itemList
}

async function addSchedule(){
    //新增默认格式的空数据
    let {data} = await request.get('schedule/addDefaultSchedule',{params:{"uid":sysUser.uid}})
    if(data.code == 200){
        alert("新增成功")
        showSchedule()
    }else{
        alert("新增失败")
    }

}

async function upadteSchedule(index){
    let {data} = await request.post('schedule/updateSchedule',sysSchedule.itemList[index])
    if(data.code == 200){
        alert("更新成功")
        showSchedule()
    }else{

        alert("更新失败")
    }
   
}

async function deleteSchedule(index){
    let {data} = await request.get('schedule/deleteSchedule',{params:{"sid":sysSchedule.itemList[index].sid}})
    if(data.code == 200){
        console.log(data)
        alert("删除成功")
        showSchedule()
    }else{
        alert("删除失败")
    }
  
        

}

</script>

<template>
  <div>
    <h3 class="ht">您的日程如下</h3>
<table class="tab" cellspacing="0px">
    <tr class="ltr">
        <th>编号</th>
        <th>内容</th>
        <th>进度</th>
        <th>操作</th>
    </tr>
    <tr class="ltr"  v-for="(item,index) in sysSchedule.itemList" :key="index">
        <td v-text="index+1"></td>
        <td>
            <input type="text" v-model="item.title">
        </td>
        <td>
            <input type="radio" v-model="item.completed" value="1"> 已完成
            <input type="radio" v-model="item.completed" value="0"> 未完成
        </td>
        <td class="buttonContainer">
             <button class="btn1" @click="deleteSchedule(index)">删除</button>
            <button class="btn1" @click="upadteSchedule(index)">保存修改</button>
        </td>
    </tr>
    <tr class="ltr buttonContainer" >
        <td colspan="4">
            <button class="btn1" @click="addSchedule()">新增日程</button>
        </td>

    </tr>
</table>
  </div>
</template>

<style scoped>

      .ht{
            text-align: center;
            color: cadetblue;
            font-family: 幼圆;
        }
        .tab{
            width: 80%;
            border: 5px solid cadetblue;
            margin: 0px auto;
            border-radius: 5px;
            font-family: 幼圆;
        }
        .ltr td{
            border: 1px solid  powderblue;

        }
        .ipt{
            border: 0px;
            width: 50%;

        }
        .btn1{
            border: 2px solid powderblue;
            border-radius: 4px;
            width:100px;
            background-color: antiquewhite;

        }
        #usernameMsg , #userPwdMsg {
            color: gold;
        }

        .buttonContainer{
            text-align: center;
        }

</style>